package com.mall.b2c.domain.store;


import com.google.common.collect.Lists;
import com.mall.b2c.bean.StoreBean;
import com.mall.b2c.bean.StoreCityBean;
import com.mall.common.domain.PriorityEntity;

import javax.persistence.*;
import java.util.List;
import java.util.Set;

/**
 * 实体类 - 门店城市
 */
@Entity
@Table(name = "b2c_store_city")
public class StoreCityDomain extends PriorityEntity{

	private String name; // 城市名称
	private String uname; // URL别名
	private String code; //城市编码
	private String xpoint;//x坐标
	private String ypoint;//y坐标
	private Boolean is_effect; //状态，是否有效
	private Boolean is_default; //是否默认

	private Set<StoreDomain> stores;

	@PrePersist
	public void prePersist() {
		if(this.getIs_default() == null){
			this.setIs_default(false);
		}
		if(this.getIs_effect() == null){
			this.setIs_effect(false);
		}
	}

	@PreUpdate
	public void preUpdate() {
		if(this.getIs_default() == null){
			this.setIs_default(false);
		}
		if(this.getIs_effect() == null){
			this.setIs_effect(false);
		}
	}
	
	@Column(length=100)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(length=100, unique=false)
	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getXpoint() {
		return xpoint;
	}

	public void setXpoint(String xpoint) {
		this.xpoint = xpoint;
	}

	public String getYpoint() {
		return ypoint;
	}

	public void setYpoint(String ypoint) {
		this.ypoint = ypoint;
	}

	@Column(columnDefinition="bit(1) not null default 0")
	public Boolean getIs_effect() {
		return is_effect;
	}

	public void setIs_effect(Boolean is_effect) {
		this.is_effect = is_effect;
	}

	@Column(columnDefinition="bit(1) not null default 0")
	public Boolean getIs_default() {
		return is_default;
	}

	public void setIs_default(Boolean is_default) {
		this.is_default = is_default;
	}

	@OneToMany(fetch = FetchType.LAZY,mappedBy = "city")
	public Set<StoreDomain> getStores() {
		return stores;
	}

	public void setStores(Set<StoreDomain> stores) {
		this.stores = stores;
	}

	@Transient
	public StoreCityBean getBean() {
		StoreCityBean bean = new StoreCityBean();
		bean.setId(this.getId());
		bean.setName(this.getName());
		bean.setCode(this.getCode());
		List<StoreBean> stores = Lists.newArrayList();
		if(getStores()!= null){
			for(StoreDomain store : getStores()){
				stores.add(store.getBean());
			}
			bean.setStores(stores);
		}
		return bean;
	}

	@Transient
	public List<StoreBean> getStoreBean(StoreBean excudleStore){
		List<StoreBean> stores = Lists.newArrayList();
		if(getStores()!= null){
			for(StoreDomain store : getStores()){
				if(excudleStore!=null && excudleStore.getId().equals(store.getId())){
					continue;
				}
				stores.add(store.getBean());
			}
		}
		return stores;
	}
}